package com.xbongbong.workflow.service.operator.impl;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.xbongbong.paas.domain.entity.ext.PaasFormDataEntityExt;
import com.xbongbong.paas.field.FieldAttrEntity;
import com.xbongbong.paas.pojo.workflow.WorkflowDataPojo;
import com.xbongbong.paas.pojo.workflow.WorkflowExplainPojo;
import com.xbongbong.paas.toolbox.exception.XbbException;
import com.xbongbong.paas.util.ExplainUtil;
import com.xbongbong.paas.util.WorkflowExplainUtil;
import com.xbongbong.pro.enums.errorcodes.SystemErrorCodeEnum;
import com.xbongbong.workflow.domain.entity.WorkflowNodeEntity;
import com.xbongbong.workflow.pojo.WorkflowOperationsPojo;
import com.xbongbong.workflow.pojo.WorkflowTransferPOJO;
import com.xbongbong.workflow.pojo.nodes.InsertRecordNode;
import com.xbongbong.workflow.service.operator.WorkflowOperatorMainAndSubFormService;
import com.xbongbong.workflow.service.operator.WorkflowOperatorService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

/**
 * 此方法主要是用来解决主字段赋值给子表单字段
 * @author 吴峰
 * @date 2021/06/24 9:45
 */
@Service("workflowOperatorSubFormMainServiceImpl")
public class WorkflowOperatorSubFormMainServiceImpl implements WorkflowOperatorMainAndSubFormService<JSONObject> {

    private static final Logger LOGGER = LoggerFactory.getLogger(WorkflowOperatorSubFormMainServiceImpl.class);

    @Resource
    private Map<Integer, WorkflowOperatorService>  workflowOperatorMap;

    @Override
    public JSONObject getInsertFieldListByDataNode(WorkflowOperationsPojo workflowOperationsPojo, WorkflowExplainPojo workflowExplainPojo, FieldAttrEntity targetField,
                                                   Map<Long, PaasFormDataEntityExt> formDataMap, Map<Long, JSONObject> dataMap, InsertRecordNode insertRecordNode,
                                                   WorkflowNodeEntity workflowNodeEntity, WorkflowDataPojo workflowDataPojo, WorkflowTransferPOJO workflowTransferPojo) throws XbbException {
        try {
            // 主字段赋值给子表单字段
            JSONObject result = new JSONObject();
            Map<Long, Map<String, Map<String, FieldAttrEntity>>> subExplainMap = workflowExplainPojo.getSubExplainMap();
            Map<String, Map<String, WorkflowOperationsPojo>>  targetSubOperator = workflowExplainPojo.getTargetSubOperator();

            List<String> targetAttrList = ExplainUtil.getAttrByPoint(workflowOperationsPojo.getAttr());
            Map<Long, Map<String, Map<String, FieldAttrEntity>>> workOrderSubExplainMap = workflowExplainPojo.getWorkOrderSubExplainMap();
            String targetAttr = targetAttrList.get(0);
            Map<String, WorkflowOperationsPojo> workflowOperationsMap = targetSubOperator.get(targetAttr);
            Iterator<String> subOperatorIterator = workflowOperationsMap.keySet().iterator();
            JSONObject resultJson = new JSONObject();
            JSONArray resultJsonArray = new JSONArray();
            while (subOperatorIterator.hasNext()) {
                String subKey = subOperatorIterator.next();
                WorkflowOperationsPojo subWorkflowOperationsPojo = workflowOperationsMap.get(subKey);
                targetAttrList = ExplainUtil.getAttrByPoint(workflowOperationsPojo.getAttr());
                FieldAttrEntity targetSubField = WorkflowExplainUtil.getTargetSubField(workOrderSubExplainMap, subExplainMap, workflowNodeEntity,
                        targetAttrList.get(0), targetAttrList.get(1));
                String thisAttr = subWorkflowOperationsPojo.getValueAttr();
                // 获取字段解释，可能为null，只有动态值才有字段，公式和固定值是无固定的字段的
                WorkflowOperatorService workflowOperatorService = workflowOperatorMap.get(subWorkflowOperationsPojo.getValueType());
                FieldAttrEntity fieldAttrEntity = WorkflowExplainUtil.getThisField(workflowDataPojo.getExplainMap(),
                        subWorkflowOperationsPojo.getFormId(), subWorkflowOperationsPojo.getBusinessType(),
                        subWorkflowOperationsPojo.getValueType(), thisAttr);

                Object value = workflowOperatorService.operatorByInsertNodeSourceData(workflowNodeEntity,targetSubField, fieldAttrEntity, formDataMap,
                        dataMap, subWorkflowOperationsPojo, workflowDataPojo, workflowTransferPojo, workflowExplainPojo);
                resultJson.put(targetAttrList.get(1), value);
            }
            resultJsonArray.add(resultJson);
            result.put(targetAttr, resultJsonArray);
            return result;
        }  catch (XbbException e) {
            throw e;
        } catch (Exception e) {
            LOGGER.error("WorkflowOperatorSubFormMainServiceImpl.getInsertFieldListByDataNode error", e);
            throw new XbbException(SystemErrorCodeEnum.API_ERROR_100001);
        }
    }

    @Override
    public Object getUpdateData(WorkflowOperationsPojo workflowOperationsPojo, WorkflowExplainPojo workflowExplainPojo, FieldAttrEntity targetField, Map<Long, PaasFormDataEntityExt> workflowDataMap,
                                Map<Long, JSONObject> dataMap, WorkflowNodeEntity workflowNodeEntity, WorkflowDataPojo workflowDataPojo, WorkflowTransferPOJO workflowTransferPojo) throws XbbException {
        try {
            Map<Long, Map<String, Map<String, FieldAttrEntity>>> subExplainMap = workflowExplainPojo.getSubExplainMap();
            Map<Long, Map<String, Map<String, FieldAttrEntity>>> workOrderSubExplainMap = workflowExplainPojo.getWorkOrderSubExplainMap();

            List<String> targetAttrList = ExplainUtil.getAttrByPoint(workflowOperationsPojo.getAttr());
            FieldAttrEntity targetSubField = WorkflowExplainUtil.getTargetSubField(workOrderSubExplainMap, subExplainMap, workflowNodeEntity,
                    targetAttrList.get(0), targetAttrList.get(1));
            // 获取字段解释，可能为null，只有动态值才有字段，公式和固定值是无固定的字段的
            String thisAttr = workflowOperationsPojo.getValueAttr();
            FieldAttrEntity fieldAttrEntity = WorkflowExplainUtil.getThisField(workflowDataPojo.getExplainMap(),
                    workflowOperationsPojo.getFormId(), workflowOperationsPojo.getBusinessType(),
                    workflowOperationsPojo.getValueType(), thisAttr);
            WorkflowOperatorService workflowOperatorService = workflowOperatorMap.get(workflowOperationsPojo.getValueType());
            return workflowOperatorService.operatorByUpdateNode(workflowNodeEntity, fieldAttrEntity, targetSubField,  workflowDataMap,
                    dataMap, workflowOperationsPojo, workflowDataPojo, workflowTransferPojo, workflowExplainPojo);
        }  catch (XbbException e) {
            throw e;
        } catch (Exception e) {
            LOGGER.error("WorkflowOperatorSubFormMainServiceImpl.getUpdateData error", e);
            throw new XbbException(SystemErrorCodeEnum.API_ERROR_100001);
        }

    }
}
